import cloud from '@tbmp/mp-cloud-sdk';
cloud.init({
  env: 'test'
});
App({
  cloud,
  globalData: {
    type: 0,
    nick: "",
    user_id: 0,
    isEmptyInfo: false,
    awardType: 0,
    goodsUrl: '',
    chance: 1,
    isMember: false
  },
  onLaunch(options) {
    // 第一次打开
    // options.query == {number:1}
    console.info('App onLaunch');
    // this.getAuthUserInfo();
  },
  onShow(options) {
    // 从后台被 scheme 重新打开
    // options.query == {number:1}
  },

  //授权
  getAuthUserInfo() {
    let that = this;

    cloud.function.invoke('mmvr', null, 'main').then(res => {
      console.log(res);
    })

    my.authorize({
      scopes: "scope.userInfo",
      success: (res) => {
        my.getAuthUserInfo({
          success: (userInfo) => {
            that.globalData.nick = userInfo.nickName;
            that.getUseInfo();
          },
          fail: () => {
            // my.exit();
          }
        });
      },
      fail: () => {
        // my.exit();
      }
    });
  },

  //获取用户信息
  async getUseInfo() {
    let that = this;

    try {
      let user_info = await this.cloud.function.invoke('mmvr', { nick: that.globalData.nick }, "getUserInfo");

      if (user_info.status == "fail") {
        my.alert({
          content: "获取信息失败，请退出重新进入"
        });

        let timmer = setTimeout(() => {
          clearTimeout(timmer);
          timmer = null;

          // my.exit();
        }, 500);
      } else {
        console.log('user_info-----------', user_info);
        that.globalData.user_id = user_info.data.user_id;
        that.globalData.chance = user_info.data.chance_num;
      }
    } catch (e) {
      console.log('getUseInfo e--------------', e);
    }
  }
});
